package com.stylefeng.guns.modular.manager.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.SqlHelper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.core.datascope.DataScope;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.modular.system.model.ManagerGoods;
import com.stylefeng.guns.modular.system.dao.ManagerGoodsMapper;
import com.stylefeng.guns.modular.manager.service.IManagerGoodsService;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author LiuXin
 * @since 2018-09-12
 */
@Service
@Transactional(readOnly = true, rollbackFor = RuntimeException.class)
public class ManagerGoodsServiceImpl extends ServiceImpl<ManagerGoodsMapper, ManagerGoods> implements IManagerGoodsService {

    /**
     * 预警
     */
    private static final Integer ALARM = 1;
    /**
     * 非预警
     */
    private static final Integer NON_ALARM = 0;

    @Override
    public Page<ManagerGoods> selectWithPage(Page<ManagerGoods> page, Wrapper<ManagerGoods> wrapper) {
        if(ShiroKit.isAdmin()){
            Page<ManagerGoods> result = this.selectPage(page,wrapper);
            return result;
        }else {
            DataScope dataScope = new DataScope(ShiroKit.getDeptDataScope());
            ManagerGoods managerGoods = wrapper.getEntity();
            List<ManagerGoods> result = this.baseMapper.selectPageWithDataScope(page,dataScope, managerGoods);
            page.setRecords(result);
            return page;
        }
    }

    @Override
    public Page<ManagerGoods> selectAlarmPage(Page<ManagerGoods> page, Wrapper<ManagerGoods> wrapper) {
        if(ShiroKit.isAdmin()){
            Page<ManagerGoods> result = this.selectPage(page,wrapper);
            return result;
        }else {
            DataScope dataScope = new DataScope(ShiroKit.getDeptDataScope());
            ManagerGoods managerGoods = wrapper.getEntity();
            List<ManagerGoods> result = this.baseMapper.selectAlarmPage(page,dataScope, managerGoods);
            page.setRecords(result);
            return page;
        }
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void batchAddGoodsInventory(Map<Long, Integer> tempMap) {
        for (Map.Entry<Long, Integer> entry : tempMap.entrySet()) {
            Long goodsId = entry.getKey();
            Integer value = entry.getValue();
            ManagerGoods managerGoods = this.selectById(goodsId);
            if(managerGoods == null) {
                continue;
            }
            //库存数量
            Integer stockNumber = managerGoods.getGoodsInventoryQuantity() !=null ? managerGoods.getGoodsInventoryQuantity() : 0;
            //库存数量加上页面新增数量
            Integer inventoryQuantity = value + stockNumber;
            //更新库存数量
            managerGoods.setGoodsInventoryQuantity(inventoryQuantity);
            //库存下限
            Integer minNumber = managerGoods.getGoodsMinNum();
            //库存数量减去下限库存 若小于50就预警
            if((inventoryQuantity - minNumber) < 50){
                managerGoods.setAlarmState(ALARM);
            }
            //更新库存数量
            this.updateById(managerGoods);
        }
    }

    @Override
    @Transactional(rollbackFor = RuntimeException.class)
    public void batchMinusGoodsInventory(Map<Long, Integer> tempMap) {
        for (Map.Entry<Long, Integer> entry : tempMap.entrySet()) {
            Long goodsId = entry.getKey();
            Integer value = entry.getValue();
            ManagerGoods managerGoods = this.selectById(goodsId);
            if(managerGoods == null) {
                continue;
            }
            Integer stockNumber = managerGoods.getGoodsInventoryQuantity() !=null ? managerGoods.getGoodsInventoryQuantity() : 0;
            Integer inventoryQuantity = stockNumber - value;
            managerGoods.setGoodsInventoryQuantity(inventoryQuantity);
            //库存下限
            Integer minNumber = managerGoods.getGoodsMinNum();
            //库存数量减去下限库存 若小于50就预警
            if((inventoryQuantity - minNumber) < 50){
                managerGoods.setAlarmState(ALARM);
            }
            //更新库存数量
            this.updateById(managerGoods);
        }
    }
}
